<?php
/**
 * Created by : PhpStorm
 * User: Shane
 * Date: 2023-05-23
 * Time: 17:17
 */

namespace app\api\controller\client;


use app\common\controller\Api;
use app\common\model\Merchantgoods;
use think\Db;

class Merchant extends Api
{

    protected $noNeedLogin = ['getMerchant'];
    protected $noNeedRight = ['*'];

    /**
     * 首页获取推荐门店
     * 目前根据最近距离获取5个店铺
     * 条件为小于且开启是否含有此类服务
     */
    public function getRecommendMerchant(){

    }
    /**
     * 获取所有的门店
     */
    public function getMerchant(){
        $page = !$this->request->param('page') ? 1 : $this->request->param('page');//页
        $list=\app\common\model\Merchant::field("id,name,logo,describe,address,lon,lat,door_image,images,start_time,end_time")
            ->where(["status"=>1])
            ->paginate(10, '', ['page' => $page, 'list_rows' => 10])
            ->each(function ($v){
                $lng1=$this->request->param('lng');$lat1=$this->request->param('lat');
                $v["goodsLists"]=Merchantgoods::field("id,name,describe,images,number,price,cost_price")
                    ->where(["type_data"=>1,"merchant_id"=>$v["id"],"status"=>1])->select();
                $v["distance"]=$this->getdistance($lng1,$lat1,$v["lon"],$v["lat"]);//根据经纬度计算距离
            });
        $this->success("成功",$list);
    }

    /**
     * @param $lng1
     * @param $lat1
     * @param $lng2
     * @param $lat2
     * @return float
     * 经纬度计算距离
     */
    private function getdistance($lng1,$lat1,$lng2,$lat2)//根据经纬度计算距离
    {
        //将角度转为狐度
        $radLat1=deg2rad($lat1);
        $radLat2=deg2rad($lat2);
        $radLng1=deg2rad($lng1);
        $radLng2=deg2rad($lng2);
        $a=$radLat1-$radLat2;//两纬度之差,纬度<90
        $b=$radLng1-$radLng2;//两经度之差纬度<180
        $s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137;
        return $s;
    }
}